<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="cn.zqy.model.BookLanguage" %>
<%@ page import="cn.zqy.model.BookStatus" %>
<%@ page import="cn.zqy.model.Library" %>
<%@ page import="cn.zqy.model.LibraryActions" %>
<%@ page import="cn.zqy.services.LibraryService" %>
<%@ page import="cn.zqy.services.Utils" %>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%
String bookStatus=request.getParameter("book_status")==null?"":request.getParameter("book_status");
String bookLanguage=request.getParameter("book_language")==null?"":request.getParameter("book_language");
String bookNo=request.getParameter("book_no")==null?"":request.getParameter("book_no");
String bookName=request.getParameter("book_name")==null?"":request.getParameter("book_name");
HashMap<String,String> paramsMap=new HashMap<String,String>();
if(bookStatus!=null && bookStatus.length()>0)
{
	paramsMap.put("bookStatus", bookStatus);
}
if(bookLanguage!=null && bookLanguage.length()>0)
{
	paramsMap.put("language", bookLanguage);
}
if(bookNo!=null && bookNo.length()>0)
{
	paramsMap.put("bookNo", bookNo);
}
if(bookName!=null && bookName.length()>0)
{
	paramsMap.put("bookName", bookName);
}
String bookStatusSelect=Utils.createHtmlSelect("book_status",BookStatus.IN.getAllDesc(),BookStatus.IN.getAllValue(),bookStatus);
String bookLanguageSelect=Utils.createHtmlSelect("book_language",BookLanguage.ALL.getAllDesc(),BookLanguage.ALL.getAllValue(),bookLanguage);
ArrayList<Library> libraryList=new LibraryService().retrieveAll(paramsMap);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MphasiS(Shanghai) Library</title><script type="text/javascript">
function addBook()
{
	//alert("here!");
	if(document.getElementById("book_no").value=="")
	{
		alert("please input the No.");
		document.getElementById("book_no").focus();
	}
	else if(document.getElementById("book_name").value=="")
	{
		alert("please input the Name.");
		document.getElementById("book_name").focus();
	}
	else
	{
		document.form1.action="/Library";
		document.form1.submit();
	}
}
</script>
</head>
<body>
<form method="post" name="form1">
<table align="center">
  <tr>
	<td>No.</td><td><input name="book_no" id="book_no" value="<%=bookNo %>" size="12"/></td>
	<td>Name</td><td><input name="book_name" id="book_name" value="<%=bookName %>"/></td>
	<td>Language</td><td><%=bookLanguageSelect %></td>
	<td>Status</td><td><%=bookStatusSelect %></td>	
<%
if(Utils.isAuthorized())
{
%>
	<td>Owner</td><td><input name="book_owner" /></td>
	<td><input type="button" value="Add" onclick="addBook();"/><input type="hidden" name="flag" value="<%=LibraryActions.ADD.toString() %>" /></td>
	<td><input type="submit" value="Query"/></td>
<%
}
else
{
%>
	<td><input type="submit" value="Query"/></td>
	<td><a target="_parent" href="library_admin.jsp">Lib Admin</a></td>
<%
}
%>
	
  </tr>
</table>
</form>
<div>
<%
if(libraryList.size()>0)
{
	out.println("<table align='left'>");
	out.println("<tr><td>No.</td><td>Name</td><td>Language</td><td>Status</td><td>Action</td></tr>");
	for(Library library:libraryList)
	{
		boolean isIn=library.getBookStatus().startsWith("I")?true:false;
		out.println("<tr>");
		if(Utils.isAuthorized())
		{
			out.println("<td><a title='Delete' target=\"_parent\" href=\"/library_update.jsp?book_no="+library.getBookNo()+"&flag="+LibraryActions.DELETE.toString()+"\">"+library.getBookNo()+"</a></td>");
			out.println("<td><a title='Update' target=\"_parent\" href=\"/library_update.jsp?book_no="+library.getBookNo()+"&flag="+LibraryActions.UPDATE.toString()+"\">"+library.getBookName()+"</a></td>");
		}
		else
		{
			out.println("<td>"+library.getBookNo()+"</td>");
			out.println("<td>"+library.getBookName()+"</td>");
		}
		out.println("<td>"+library.getLanguage()+"</td><td>"+library.getBookStatus()+"</td><td>");
		String flag=LibraryActions.RESERVE.toString();
		String action="To Reserve";
		if(Utils.isAuthorized())
		{//for administrator
			if(isIn)
			{
				flag=LibraryActions.LEND.toString();
				action="To Lend";
				out.println("<a target=\"_parent\" href=\"/library_update.jsp?book_no="+library.getBookNo()+"&flag="+flag+"\">"+action+"</a></td></tr>");
			}
			else
			{
				flag=LibraryActions.RETURN.toString();
				action="To Return";
				out.println("<a target=\"_parent\" href=\"/Library?book_no="+library.getBookNo()+"&flag="+flag+"\">"+action+"</a></td></tr>");
			}
			
		}
		else
		{//for others
			if(isIn)
			{
				out.println("&nbsp;</td></tr>");
			}
			else
			{
				if(library.getContact()!=null&&library.getContact().length()>0)
				{
					out.println("Reserved</td></tr>");
				}
				else
				{
					out.println("<a href=\"/library_update.jsp?book_no="+library.getBookNo()+"&flag="+flag+"\">"+action+"</a></td></tr>");
				}				
			}
		}
	}
	out.println("</table>");
}
else
{
%>
<p>No data found!</p>
<%
} 
%>
</div>
</body>
</html>